package com.t194.controller;

import com.t194.pojo.User;
import com.t194.service.UserService;
import com.t194.util.Log;
import com.t194.vo.PageResult;
import com.t194.vo.UserDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class UserController {

        private String msg;
        @Resource
        private UserService userService;

        /**
         * 使用非ajax 时用ModelAndView
         * @return
         */
        @GetMapping("/index")
        public ModelAndView index(){
                ModelAndView mav=new ModelAndView();
                mav.setViewName("sys/user");
                return mav;
        }
        @GetMapping("/query")
        public PageResult query(Integer offset, Integer limit, UserDto userDto){
             return userService.searchPagination(offset,limit,userDto);
        }
        @RequestMapping("/grant")
        public String grant(@RequestParam Integer userId, Integer roleId){
                try {
                        userService.grant(userId,roleId);
                        msg="用户授权成功";
                }catch (Exception ex){
                        msg="用户授权失败";
                        Log.logger.error(msg,ex);
                }
                return msg;
        }

}
